<?php
ob_start();  
session_start();
 
     $servizio = $_GET['service'];
     
     $access_token =  $_GET['access_token'];
     
     //echo$access_token;
    // break;
     
    //retrive base url 
    $url = $_SESSION['basic_url'];
    
    
     //url avatar
     $urlimg =  $_GET['urlimg'];
     
try{     
     
     $ris = InsertToken($_SESSION['id'],$access_token,$servizio,$urlimg);
     
      //retrieve public pages and display it
        if($servizio=="Facebook"){

            //add service url
            $service_url="/GetPublicPage?";
            //params url
            $params = array('id'=>$_SESSION['id'],'service'=>$servizio);
            //send GET request
            $output = @file_get_contents($url.$service_url."id=".$params['id']."&"."service=".$params['service'],0,$ctx);
            //parse JSON output
           // print_r($url.$service_url."id=".$params['id']."&"."service=".$params['service']); break;
            $data = json_decode($output);
            $pageResult = $data->GetPublicPageResult;

            //print_r($pageResult[0]);
            
             ?>

            <form action="insert_token_page.php" method="get">
            <select name="access_token_page" id="access_token_page">
            <?php    
            
        
            for($i=0;$i<10;$i++){
                $split = explode(";",$pageResult[$i]);
                    $name_page[$i]=$split[0];
                    $access_token_page[$i]=$split[1];
                    if($name_page[$i]!= null){
                ?>   
                    <option value="<?php echo$access_token_page[$i]; ?>" ><?php echo$name_page[$i]; ?></option>;
                <?php 
                    }
            }           
            ?>
            </select> 
            <input type="submit" value="Choose Your Company Page to add ESA system" id="choose_page"/>
            </form>
            <?php    
            

        }else{ ?>
       
            <script>
                        
                window.opener.location.reload(); 
                setTimeout('window.close()',100);
                        
             </script>
  <?php }
     
}catch(Exception $e){

  echo"Connection Error Insert Token";
}
         
 
function InsertToken($idutente,$access_token,$servizio,$urlimg){

                 
                        
                    $file=fopen("../setting.txt","r");

                    $dynamic_url = fgets($file);
                    $protocol="http://";
                    $static_url="/ServiceRest.svc";

                    //set server connection
                    $url = $protocol.$dynamic_url.$static_url;
                // echo$url; 
                    $_SESSION['basic_url'] = $url;

                // echo$url ; break;
                    $ctx = stream_context_create(array('http' => array('timeout' => 1)));

                    $client = @file_get_contents($url,0,$ctx);
                    
                    if(isset($client)){
                     
                        //retrive base url 
                        $url = $_SESSION['basic_url'];
                        //add service url
                        $service_url="/InsertToken?";
                        //params url
                        $params = array('id'=>$_SESSION['id'],'access_token'=>$access_token,'service'=>$servizio,'url'=>$urlimg);
                        //params connection
                        $ctx = stream_context_create(array('http' => array('timeout' => 1))); 
                        //send GET request
                        $output = @file_get_contents($url.$service_url."id=".$params['id']."&"."access_token=".$params['access_token']."&"."service=".$params['service']."&"."url=".$params['url'],0,$ctx);
                        //parse JSON output
                        $data = json_decode($output);
                        $webResult = $data->InsertTokenResult;
                        
                        return $webResult;
                    }   
                        return "error insert token";
                     
                    
                     
}   


       
?>

